let userNickname = null;
let userProvince = null;
let userCity = null;
let userSex = null;
let userBirthday = null;
let userSignature = null;
let imgEmdImg = null;
let userImgSrc = null;
let backBtn = null;
let submitBtn = null;


$(function () {
    userNickname = $('#userNickname');
    userProvince = $('#userProvince');
    userCity = $('#userCity');
    userSex = $('#userSex');
    userBirthday = $('#datetimepicker');
    userSignature = $('#userSignature');
    imgEmdImg = $('#imgEmdImg');
    userImgSrc = $('#userImageSrc');
    backBtn = $('#backButton');
    submitBtn = $('#userInfo-Modify-btn');


    userNickname.on("focus", function () {
        //有友情提示连接
        validateTip(userNickname.next(), {"color": "#666666"}, "* 用户昵称长度必须大于1小于10", false);
    }).on("blur", function () {
        if (userNickname.val() != null && userNickname.val().length > 1 && userNickname.val().length < 10) {
            validateTip(userNickname.next(), {"color": "green"}, imageYes, true);
        } else {
            validateTip(userNickname.next(), {"color": "red"}, imageNo + " 用户昵称不符合规范，请重新输入", false);
        }
    }).focus();


    /**
     * 当选择了不同的城市后，ajax异步交互加载该省份对应的城市
     */
    userProvince.on("change", function () {
        $.ajax({
            type: "GET",
            url: path + "/user/getCity",
            data: {"provinceid": userProvince.val()},
            dataType: "json",
            success: function (data) {
                if (data != null) {
                    userCity.html("");
                    let options = "<option value= \"0\" disabled selected>请选择城市</option>";
                    for (let i = 0; i < data.length; i++) {
                        options += "<option value=\"" + data[i].id + "\">" + data[i].name + "</option>";
                    }
                    userCity.html(options);
                }
            }
        })
    });


    // 初始化 日期插件
    userBirthday.datetimepicker({
        defaultDate:new Date(),
        maxDate:0, //  今天
        timepicker:false,
        format:'Y-m-d',
        validateOnBlur:false
    }).on("blur",function () {
        let r=new RegExp(/^(\d{4})-(0\d{1}|1[0-2])-(0\d{1}|[12]\d{1}|3[01])$/);
        if(!(r.test(userBirthday.val()))){
            validateTip(userBirthday.next(),{"color":"red"},imageNo+" 日期格式不正确",false);
        } else {
            validateTip(userBirthday.next(),{"color":"green"},imageYes ,true);
        }
    });
    $.datetimepicker.setLocale('zh');//使用中文


    userImgSrc.on("change", function () {
        if (this.files.length > 0) {
            let file = this.files[0];
            let reader = new FileReader();

            reader.onload = function (e) {
                document.querySelector('#imgEmdImg').src = this.result;
            }

            reader.readAsDataURL(file);
        }
    });

    // 点击头像，新窗口中展示
    $("#bloggerImage").on("click", function () {
        let src = $(this).attr("src");
        window.open(src);
    })


    submitBtn.on("click", function () {
        if (userNickname.attr("validateStatus")!="true") {
            userNickname.blur();
        } else if (confirm("确认信息无误了吗?")) {
            $('#InfoForm').submit();
        }
    });




    backBtn.on("click", function () {
        if (referer != undefined
            && null != referer
            && "" != referer
            && "null" != referer
            && referer.length > 4) {
            window.location.href = referer;
        } else {
            history.back(-1);
        }
    });
})